package com.changanw.jedis;

import redis.clients.jedis.params.SetParams;


import java.util.Arrays;
import java.util.UUID;

public class LuaTest {
    public static void main(String[] args) {
        Redis redis = new Redis();
        for (int i = 0; i < 2 ; i++) {
            String KEY = "name"+i;
            redis.execute(jedis -> {
                //1、先获取一个随机数
                String value = UUID.randomUUID().toString();
                //2、获取锁
                String k1 = jedis.set("k1",value,new SetParams().nx().px((long) 1));
                //3、判断是否拿到锁
                if (k1 != null && "OK".equals(k1)){
                    //4、具体的业务
                    jedis.set(KEY,"changanw.com");
                    System.out.println(jedis.mget("name0","name1"));
                    //5、调用脚本释放锁
                    jedis.evalsha("b8059ba43af6ffe8bed3db65bac35d452f8115d8", Arrays.asList("k1"),Arrays.asList(value));
                }else {
                    System.out.println("没有拿到锁");
                }
            });
        }
    }
}
